package com.dxy.dxyadmin.repository;

import com.dxy.dxyadmin.DO.Admin;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * junfeng 2018/11/6 管理员持久层
 */
public interface AdminRepository extends JpaRepository<Admin, Integer> {

    /**
     * 通过用户名判断是否重复
     *
     * @param name
     * @return
     */
    boolean existsByName(String name);

    /**
     * 更新管理员状态
     *
     * @param id
     * @param status
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update `um_admin` set status =:status  where id = :id", nativeQuery = true)
    int updateStatusById(@Param("id") Integer id, @Param("status") Integer status);

    /**
     * 查找所有管理员
     *
     * @param specification
     * @param pageable
     * @return
     */
    Page<Admin> findAll(Specification<Admin> specification, Pageable pageable);

    /**
     * 通过姓名查找管理员
     *
     * @param name
     * @return
     */
    Admin findByNameAndStatus(String name, Integer code);

    /**
     * 通过名称查找管理员
     *
     * @param name
     * @return
     */
    Admin findByName(String name);


}
